package com.xnyzc.lhy.mis.controller.cmsapi.approval;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.approval.ApplyAmountParam;
import com.xnyzc.lhy.mis.service.cms.approval.ICmsRaiseApplyService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Description 额度审批
 * @Author zhuyubin
 * @Date 2019-08-27 15:02
 **/
@RestController
@RequestMapping(value = "cmsapi/cmsRaiseApply")
public class CmsRaiseApplyController {

    @Autowired
    private ICmsRaiseApplyService iCmsRaiseApplyService;

    /**
     * 申请提额列表
     * @param name 用户名
     * @param phone 用户手机号
     * @param officeId 机构名
     * @return
     */
    @GetMapping(value = "selectApplyAmountPage")
    public Rv selectApplyAmountPage(
            @ApiParam(name = "current", value = "当前页", required = true) Long current,
            @ApiParam(name = "name", value = "用户名") String name,
            @ApiParam(name = "phone", value = "用户手机号") String phone,
            @ApiParam(name = "officeId", value = "机构id") String officeId){
        Long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        // TODO 暂时不做校验
        List<String> type = TokenUtil.getRoleDataScope();
        return Rv.wrap(iCmsRaiseApplyService.selectApplyAmountPage(sysUserId, type, tag, current, name, phone, officeId));
    }

    /**
     * 通过审批
     * @return
     */
    @PostMapping(value = "passApplyAmount")
    public Rv passApplyAmount(@ApiParam(name = "applyAmountParam", value = "审批入参") @RequestBody ApplyAmountParam applyAmountParam){
        Long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        String raiseId = applyAmountParam.getRaiseId();
        if (CheckUtil.objIsEmpty(raiseId)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(EErrorCode.success, iCmsRaiseApplyService.passApplyAmount(sysUserId, tag, raiseId), new HashMap<>(16));
    }

    /**
     * 驳回审批
     * @return
     */
    @PostMapping(value = "rejectApplyAmount")
    public Rv rejectApplyAmount(@ApiParam(name = "applyAmountParam", value = "审批入参") @RequestBody ApplyAmountParam applyAmountParam){
        Long sysUserId = TokenUtil.getTokenUserId();
        String raiseId = applyAmountParam.getRaiseId();
        String approvalOpinion = applyAmountParam.getApprovalOpinion();
        if (CheckUtil.objIsEmpty(raiseId, approvalOpinion)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(EErrorCode.success, iCmsRaiseApplyService.rejectApplyAmount(sysUserId, raiseId, approvalOpinion), new HashMap<>(16));
    }
}
